
from tkinter import *
import threading
import socket

PORT = 9991
# 创建socket
clientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 连接服务器
clientSocket.connect(("127.0.0.1",PORT))

m = ""

class MyThread3(threading.Thread):

    def __init__(self,s):
        threading.Thread.__init__(self)
        self.s = s

    def run(self):
        global m
        while True:
            data = self.s.recv(1024)
            if data:
                print(data)
                m += data.decode() + "\n"
                message.config(text=m)
            else:
                break

t = MyThread3(clientSocket)
t.start()

# 发送信息
# msg = input("请输入要发送的信息：")
# clientSocket.send(msg.encode("utf-8"))

# 接受信息
# print("接收服务器发回的信息")
# data = clientSocket.recv(1024)
# print("接收到信息",data.decode())

# 关闭连接
# clientSocket.close()



root = Tk()
root.title("客户端")
root.geometry("300x400")
message = Message(root,text="aaa\nbbb",width=200)
message.pack()

def send():
    print(entry.get())
    clientSocket.send(entry.get().encode("utf-8"))

entry = Entry(root)
entry.pack()
button1 = Button(root,text="Send",command=send)
button1.pack()
button = Button(root,text="Exit",command=root.quit)
button.pack()

root.mainloop()